package com.yixing.tech.system.controller;

import com.yixing.tech.common.base.*;
import com.yixing.tech.common.service.CommonService;
import com.yixing.tech.system.service.ITenantService;
import com.yixing.tech.common.vo.s3.TenantVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.yixing.tech.common.security.context.UserContext.getTenantId;

/**
 * @author yixing tech
 * at 2021/12/20
 * describe：
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/tenant")
public class TenantController extends BaseControl {

    @Resource
    ITenantService tenantService;
    @Autowired
    private CommonService commonService;

    @PostMapping("/add")
    public Rs<Object> add(@RequestBody TenantVO vo) throws BusinessException {
        commonService.fillCreateUser(vo);
        vo.setParentId(getTenantId());
        tenantService.addChild(vo);
        return Rs.ok();
    }

    @PostMapping("/add/child")
    public Rs<Object> addChild(@RequestBody TenantVO vo) throws BusinessException {
        commonService.fillCreateUser(vo);
        tenantService.addChild(vo);
        return Rs.ok();
    }

    @PostMapping("/list")
    public Rs<Object> list(@RequestBody TenantVO vo) throws BusinessException {
        vo.setParentId(getTenantId());
        List<TenantVO> list = tenantService.list(vo);
        return Rs.ok(list);
    }

    @PostMapping("/listPage")
    public Rs<Object> listPage(@RequestBody PageRow<TenantVO> vo) throws BusinessException {
        vo.getCondition(TenantVO.class).setParentId(getTenantId());
        Page<TenantVO> page = tenantService.list(vo);
        return Rs.ok(page);
    }

    @GetMapping("/{id}/admin/list")
    public Rs<Object> adminList(@PathVariable String id) throws BusinessException {
        return Rs.ok(tenantService.getAdminList(id));
    }

    @PostMapping("/list/whole")
    public Rs<Object> listWhole(@RequestBody TenantVO vo) throws BusinessException {
        vo.setTenantId(getTenantId());
        List<TenantVO> list = tenantService.list(vo);
        return Rs.ok(list);
    }

    @GetMapping("")
    public Rs<Object> get() throws BusinessException {
        return Rs.ok(tenantService.queryById(getTenantId()));
    }
    @GetMapping("/{id}")
    public Rs<Object> get(@PathVariable String id) throws BusinessException {
        commonService.getApiUser();
        return Rs.ok(tenantService.queryById(id));
    }

    @GetMapping("/{tenantId}/freeze")
    public Rs<Object> freeze(@PathVariable String tenantId) throws BusinessException {
        tenantService.freezeOrUnFreeze(tenantId, true);
        return Rs.ok();
    }

    @GetMapping("/{tenantId}/unfreeze")
    public Rs<Object> unfreeze(@PathVariable String tenantId) throws BusinessException {
        tenantService.freezeOrUnFreeze(tenantId, false);
        return Rs.ok();
    }


    @PostMapping("/update/{tenantId}")
    public Rs<Object> updateTenant(@RequestBody TenantVO vo, @PathVariable String tenantId) throws BusinessException {
        commonService.fillUpdateUser(vo);
        vo.setId(tenantId);
        tenantService.update(vo);
        return Rs.ok();
    }

    @PostMapping("/update")
    public Rs<Object> update(@RequestBody TenantVO vo) throws BusinessException {
        commonService.fillUpdateUser(vo);
        vo.setId(commonService.getApiUser().getTenantId());
        tenantService.update(vo);
        return Rs.ok();
    }

    @GetMapping("/sync/wxPublicInfo")
    public Rs<Object> syncWxPublicInfo() throws BusinessException {
        tenantService.syncWxPublicInfo(getTenantId());
        return Rs.ok();
    }


}
